package com.fable.ntzw_ga.controller;


import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fable.common.eunm.AppHttpCodeEnum;
import com.fable.common.vo.PageVO;
import com.fable.common.vo.ResultVO;
import com.fable.ntzw_ga.entity.Dept;
import com.fable.ntzw_ga.service.IDeptService;
import javax.validation.constraints.NotNull;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author Created by gzh 2023-02-03 12:41:05
 */
@RestController
@RequestMapping("/ntzw_ga/dept")
@Slf4j
@Api(tags = "部门管理")
public class DeptController {

    @Autowired
    IDeptService iDeptService;

    @ApiOperation("分页显示所有数据")
    @PostMapping
    public ResultVO showDept(@RequestBody Dept dept, PageVO pageVO) {
        return ResultVO.okResult(iDeptService.getPage(pageVO,dept));
    }

    @ApiOperation("查询数据")
    @PostMapping("searchDept")
    public ResultVO searchDept(@RequestBody Dept dept) {
        LambdaQueryWrapper<Dept> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Dept::getSeq,dept.getSeq()).or().like(Dept::getName,dept.getName());
        return ResultVO.okResult(iDeptService.list(wrapper));
    }

    @ApiOperation("获取修改数据id")
    @GetMapping("selectDept/{id}")
    public ResultVO selectDept(@PathVariable(name = "id") int id) {
        Dept dept = iDeptService.getById(id);
        return dept != null ? ResultVO.okResult(dept):ResultVO.errorResult(AppHttpCodeEnum.ERROR);
    }

    @ApiOperation("添加一条数据")
    @PostMapping("add")
    public ResultVO add(@RequestBody Dept dept) {
        Dept d = new Dept();
        d.setName(dept.getName());
        d.setSeq(dept.getSeq());
        d.setGmtCreate(LocalDateTime.now());
        return iDeptService.save(d) ? ResultVO.okResult(dept):ResultVO.errorResult(AppHttpCodeEnum.ERROR);
    }

    @ApiOperation("修改一条数据")
    @PutMapping("update/{id}")
    public ResultVO update(@PathVariable(name = "id") int id, @RequestBody Dept dept) {
        Dept byId = iDeptService.getById(id);
        byId.setName(dept.getName());
        byId.setSeq(dept.getSeq());
        byId.setGmtModified(dept.getGmtModified());
        return iDeptService.updateById(byId) ? ResultVO.okResult():ResultVO.errorResult(AppHttpCodeEnum.ERROR);
    }

    @ApiOperation("删除一条数据")
    @DeleteMapping("delete/{id}")
    public ResultVO delete(@PathVariable(name = "id") int id) {
        return iDeptService.removeById(id) ? ResultVO.okResult():ResultVO.errorResult(AppHttpCodeEnum.ERROR);
    }

    @ApiOperation("导出Excel")
    @GetMapping("/outputExcel")
    public void showAll(@NotNull HttpServletResponse response) {
        List<Dept> deptList = iDeptService.list();
        String fileName = "部门" + ".xls";
        response.setContentType("application/vnd.ms-excel; charset=utf-8");
        try {
//            response.setHeader("Content-Disposition", "attachment;Filename=" + new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
            response.setHeader("Content-Disposition", "attachment;Filename=" + URLEncoder.encode(fileName, "UTF-8"));
            EasyExcel.write(response.getOutputStream(), Dept.class).sheet("写入").doWrite(deptList);//阿里EasyExcel
        } catch (IOException e) {
            log.error("导出错误", e);
        }
    }
}
